﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using FaWorksSolution.MvcApplication.Models.Sitemap;

namespace FaWorksSolution.MvcApplication.API.Controllers
{
    public class DomainController : ApiControllerBase
    {
        // GET api/Domain
        public IEnumerable<Domain> GetDomains()
        {
            return db.SitemapDomains.AsEnumerable();
        }

        // GET api/Domain/5
        public Domain GetDomain(string id)
        {
            var domain = db.SitemapDomains.Include("Pages.Pages.Pages.Pages.Pages.Pages.Pages.Pages.Pages")
                .FirstOrDefault(m => m.DomainName == id);
            if (domain == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return domain;
        }

        // PUT api/Domain/5
        public HttpResponseMessage PutDomain(Guid id, Domain domain)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != domain.DomainId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(domain).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Domain
        public HttpResponseMessage PostDomain(Domain domain)
        {
            if (ModelState.IsValid)
            {
                domain.DomainId = Guid.NewGuid();
                db.SitemapDomains.Add(domain);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, domain);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = domain.DomainId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/Domain/5
        public HttpResponseMessage DeleteDomain(Guid id)
        {
            Domain domain = db.SitemapDomains.Find(id);
            if (domain == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.SitemapDomains.Remove(domain);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, domain);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private DefaultContext db = new DefaultContext();
    }
}